package com.example.administrator.x3picturetransliteration.Home.Presenter.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Base.HttpGo;
import com.example.administrator.x3picturetransliteration.Bean.HttpDataBean;
import com.example.administrator.x3picturetransliteration.Constants;
import com.example.administrator.x3picturetransliteration.Dao.Rx.UserRx;
import com.example.administrator.x3picturetransliteration.Dao.Table.User;
import com.example.administrator.x3picturetransliteration.Home.Activity.AppActivity;

import java.util.HashMap;
import java.util.Map;

import rx.subscriptions.CompositeSubscription;

/**
 * 登录
 * Created by Administrator on 2019/9/2.
 */

public class RegisterPresenter extends BasePresenter{

    public RegisterPresenter(Context context, CompositeSubscription mCompositeSubscription) {
        super(context, mCompositeSubscription);
    }

    /** 获取验证码 */
    public void sendRegisterSmsCode(Map<String,String> httpdata, final Callback mCallback){


        mCompositeSubscription.add(

                mHttpGo.postSubscription("sms_code/sendRegisterSmsCode", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            mCallback.getData(null);
                        }
                    }
                })
        );
    }

    /** 注册 */
    public void register(final Map<String,String> httpdata){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user/register", httpdata, "注册成功",new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()==null)return;

                            Map<String,String> map = (Map<String, String>)httpDataBean.getData();
                            updateUserDao(map.get("token"),httpdata.get("mobile"),httpdata.get("password"));

                        }

                    }
                })
        );

    }

    /** 注册成功之后将用户信息写入sqlint */
    private void updateUserDao(final String token, String userId, String Ped){

        User user = new User();
        user.setUserid(userId);
        user.setPassword(Ped);
        user.setData(token);

        Constants.getConstants().setToken(token);


        new UserRx().saveData(user, new UserRx.CallbackUserRx() {
            @Override
            public void Method() {

                Constants.getConstants().setToken(token);
                context.startActivity(new Intent(context,AppActivity.class));
                ((Activity)context).finish();
            }
        });
    }

    /** 获取用户协议 */
    public void Agreement(String type,final Callback mCallback){

        Map<String,String> httpdata = new HashMap<>();

        httpdata.put("id",type);

        mCompositeSubscription.add(

                mHttpGo.postSubscription("sys_protocol/get", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()==null)return;

                            Map<String,String> map = (Map<String,String>)httpDataBean.getData();

                            mCallback.getData(map.get("content"));
                        }
                    }
                })
        );

    }
}
